﻿using System;
using System.Collections.Generic;
using System.Threading;

namespace MyBasicLib.Collections.Generic
{ 
    public class ReaderWriterCache<TKey, TValue> : ICache<TKey, TValue>  
    {
        private readonly Dictionary<TKey, TValue> _cache;
        protected  readonly ReaderWriterLockSlim rwLock = new ReaderWriterLockSlim();

        public ReaderWriterCache()
            : this(null)
        {
        }

        public ReaderWriterCache(IEqualityComparer<TKey> comparer)
        {
            _cache = new Dictionary<TKey, TValue>(comparer);
        }

        protected Dictionary<TKey, TValue> Cache
        {
            get { return _cache; }
        }

        #region ICache<TKey,TValue> Members

        public int Count
        {
            get
            {
                rwLock.EnterReadLock();
                try
                {
                    return _cache.Count;
                }
                finally
                {
                    rwLock.ExitReadLock();
                }
            }
        }

        public TValue FetchOrCreateItem(TKey key, Func<TKey, TValue> creator)
        {
            // first, see if the item already exists in the cache
            rwLock.EnterReadLock();
            try
            {
                TValue existingEntry;
                if (_cache.TryGetValue(key, out existingEntry))
                {
                    return existingEntry;
                }
            }
            finally
            {
                rwLock.ExitReadLock();
            }
            if (creator == null) return default(TValue);

            // insert the new item into the cache

            rwLock.EnterWriteLock();
            try
            {
                TValue existingEntry;
                if (_cache.TryGetValue(key, out existingEntry))
                {
                    // another thread already inserted an item, so use that one
                    return existingEntry;
                }
                TValue newEntry = creator(key);
                _cache[key] = newEntry;
                return newEntry;
            }
            finally
            {
                rwLock.ExitWriteLock();
            }
        }

        #endregion

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        } 

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // dispose managed resources
                rwLock.Dispose();
            }
            // free native resources
        }


        public virtual void InvalidateItem(TKey key)
        {
            rwLock.EnterWriteLock();
            try
            {
                _cache.Remove(key);
            }
            finally
            {
                rwLock.ExitWriteLock();
            }
        }
    }



}